package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"all"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 更新用户信息
     *
     * @param userInfoVo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "更新用户信息")
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody UserInfoVo userInfoVo) {
        //  获取到用户Id
        Long userId = AuthContextHolder.getUserId();
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userInfoVo, userInfo);
        userInfo.setId(userId);
        //  执行更新方法
        userInfoService.updateById(userInfo);
        return Result.ok();
    }

    /**
     * 根据用户Id获取用户信息
     *
     * @param userId
     * @return
     */
    @Operation(summary = "根据用户id获取用户信息")
    @GetMapping("getUserInfoVo/{userId}")
    public Result<UserInfo> getUserInfoVo(@PathVariable Long userId) {
        //获取用户信息
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }


    /**
     * 判断用户是否购买声音列表
     *
     * @param albumId
     * @param trackIdList
     * @return
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "判断用户是否购买声音列表")
    @PostMapping("userIsPaidTrack/{albumId}")
    public Result<Map<Long, Integer>> userIsPaidTrack(@PathVariable Long albumId, @RequestBody List<Long> trackIdList) {
        //获取用户Id
        Long userId = null == AuthContextHolder.getUserId() ? 1l : AuthContextHolder.getUserId();
        //调用服务层方法
        Map<Long, Integer> map = userInfoService.userIsPaidTrack(userId, albumId, trackIdList);
        //返回map 集合数据
        return Result.ok(map);
    }

    /**
     * 判断用户是否购买过专辑
     *
     * @param albumId
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "判断用户是否购买过专辑")
    @GetMapping("isPaidAlbum/{albumId}")
    public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
        //获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //调用服务层方法
        Boolean flag = userInfoService.isPaidAlbum(userId, albumId);
        return Result.ok(flag);
    }

    /**
     * 根据专辑Id 获取到用户已支付声音Id列表
     *
     * @param albumId
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "根据专辑Id 获取到用户已支付声音Id列表")
    @GetMapping("/findUserPaidTrackList/{albumId}")
    public Result findUserPaidTrackIdList(@PathVariable Long albumId) {
        //获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //根据用户id与专辑Id查询已支付声音Id列表
        List<Long> list = userInfoService.findUserPaidTrackIdList(userId, albumId);
        //返回数据
        return Result.ok(list);
    }

    /**
     * 处理用户购买记录
     * @param UserPaidRecordVo
     * @return
     */
    @Operation(summary = "处理用户购买记录")
    @PostMapping("/savePaidRecord")
    public Result savePaidRecord(@RequestBody UserPaidRecordVo UserPaidRecordVo) {
        //调用服务层方法
        userInfoService.userPayRecord(UserPaidRecordVo);
        //返回结果
        return Result.ok();
    }

    /**
     * 更新Vip到期失效状态
     * @return
     */
    @Operation(summary = "更新Vip到期失效状态")
    @GetMapping("updateVipExpireStatus")
    public Result updateVipExpireStatus() {
        userInfoService.updateVipExpireStatus();
        return Result.ok();
    }
}

